package com.F04.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.F04.actionForm.F0406ActionForm;
import com.F04.dao.F0406Dao;
import com.action.BaseAction;
import com.config.Constants;
import com.exception.SelectException;
import com.util.CommonTools;
import com.util.SessionUtils;

public class F0406Action extends BaseAction{

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		F0406ActionForm actionForm = (F0406ActionForm) form;
		F0406Dao dao = new F0406Dao();
		String operationType = actionForm.getOperationType();
		Logger log = Logger.getLogger(F0406Dao.class);
		log.info(operationType);
		String language = SessionUtils.getLocale(request);
		actionForm.setLanguage(language);
//		供應商所屬家族
		actionForm.setList_pc_material_family(dao.List_pc_material_family());
		actionForm.setList_t_measure_units(dao.List_t_measure_units());
		try {
			/*--------------------------------------------主頁面操作begin--------------------------------------------------*/			
			if(Constants.QUERYLIST.equals(operationType)){
				List list = dao.select(actionForm);
				if(list != null){
					actionForm.setList(list);
				}else{
					actionForm.setList(new ArrayList());
				}
				return mapping.findForward(Constants.List);
			}
	
	//		初始化
			if(Constants.INIT.equals(operationType)){
				this.doINIT(actionForm);
				return mapping.findForward(Constants.List);
			}
			
//			新增
			if(Constants.ADD.equals(operationType)){
				this.initActionForm(actionForm);
				actionForm.setErrorFlag("");
				return mapping.findForward(Constants.QUERYDETAIL);
			}
			
//			编辑
			if(Constants.EDIT.equals(operationType)){
				actionForm.setMaterialCode(CommonTools.null2String(request.getParameter("code")));
				dao.selectByCode(actionForm);
				actionForm.setErrorFlag("");
				return mapping.findForward(Constants.QUERYDETAIL);
			}
			
//			查看明细页面
			if("show".equals(operationType)){
				actionForm.setMaterialCode(CommonTools.null2String(request.getParameter("code")));
				dao.selectByCode(actionForm);
				actionForm.setErrorFlag("");
				return mapping.findForward(Constants.QUERYDETAIL);
			}
			
//			删除
			if(Constants.DELETE.equals(operationType)){
				dao.delete(actionForm);
				return mapping.findForward("operateReturnList");
			}
			
			/*--------------------------------------------主頁面操作end--------------------------------------------------*/	
			
			
			/*--------------------------------------------明細頁面操作begin--------------------------------------------------*/
						
//			新增保存
			if(Constants.SAVE_INSERT.equals(operationType)){
				dao.insert(actionForm);
				return mapping.findForward("operateReturnList");
			}
			
//			编辑保存
			if(Constants.SAVE_UPDATE.equals(operationType)){
				dao.update(actionForm);
				return mapping.findForward("operateReturnList");
			}
			
//			回显(新增和編輯保存后，返回主頁面)
			if("operateReturnList".equals(operationType)){
				actionForm.setMaterialName("");
				actionForm.setMateralFamily("");
				List list = dao.select(actionForm);
				if(list != null){
					actionForm.setList(list);
				}else{
					actionForm.setList(new ArrayList());
				}
				this.initActionForm(actionForm);
				actionForm.setErrorFlag("0");
				return mapping.findForward(Constants.List);				
			}
			
//			返回
			if(Constants.back.equals(operationType)){
				this.initActionForm(actionForm);
				actionForm.setErrorFlag("");
				return mapping.findForward(Constants.back);
			}
			
//			明细页面返回主页面
			if("detailBack".equals(operationType)){
				this.initActionForm(actionForm);
				actionForm.setErrorFlag("");
				return mapping.findForward(Constants.List);
			}
			
			// 动态获取供应商的名称和编码
			if (Constants.AJAX.equals(operationType)) {
				this.initList( dao, request, response);
				return null;
			}
			/*--------------------------------------------明細頁面操作end--------------------------------------------------*/
			
			return mapping.findForward(Constants.List);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			this.initActionForm(actionForm);
			actionForm.setErrorFlag("3");
			dao.rollback();
			return mapping.findForward(Constants.List);
		}finally{
			if (dao != null)
				dao.disConnect();			
		}
	}
	
	/**
	 * @初始�?
	 */
	public void doINIT(F0406ActionForm actionForm) throws SelectException {
		actionForm.setCurrentPage(1);
	}	
	
	public void initActionForm(F0406ActionForm actionForm){
		actionForm.setDeliveryTime1(0);
		actionForm.setDeliveryTime2(0);
		actionForm.setDeliveryTime3(0);
		actionForm.setDeliveryTime4(0);
		actionForm.setDescription("");
		actionForm.setPrice1(0);
		actionForm.setPrice2(0);
		actionForm.setPrice3(0);
		actionForm.setPrice4(0);
		actionForm.setSupplierCode1("");
		actionForm.setSupplierCode2("");
		actionForm.setSupplierCode3("");
		actionForm.setSupplierCode4("");
		actionForm.setSupplierName1("");
		actionForm.setSupplierName2("");
		actionForm.setSupplierName3("");
		actionForm.setSupplierName4("");
		actionForm.setMaterialCode("");
		actionForm.setMateralFamily("");
		actionForm.setMeasuringUnit("");
		actionForm.setMaterialName("");
		actionForm.setFamilyName("");
		actionForm.setMeasuringUnitName("");
	}
	
	public void initList( F0406Dao dao,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			String id = CommonTools.trim(request.getParameter("id"));
			String name = CommonTools.trim(request
					.getParameter("name"));
//			材料
			if (id.equals("materialName")) {
				this.setFilterListIn5(dao.filterListIn5(name), response);
//			供应商
			}else if (id.equals("supplierName")) {
				this.setFilterList(dao.filterList("pc_suppliers", "code", "supplier","supplier like '%" + name + "%'"), response);
			}			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void setFilterListIn5(List list,HttpServletResponse response) throws IOException{
		StringBuffer responseXML = new StringBuffer();
		Map map = null;
		
		response.setContentType("text/xml");
		response.setCharacterEncoding("UTF-8");

		if (list != null && list.size() > 0) {
			responseXML.append("{success:true,data:{list:[");
			for (int i = 0; i < list.size(); i++) {
				map = (Map) list.get(i);
				responseXML.append("['");
				responseXML.append((String) map.get("value1"));
				responseXML.append("','");
				responseXML.append((String) map.get("value2"));
				responseXML.append("','");
				responseXML.append((String) map.get("value3"));
				responseXML.append("','");
				responseXML.append((String) map.get("value4"));
				responseXML.append("','");
				responseXML.append((String) map.get("value5"));
				if (i == list.size() - 1) {
					responseXML.append("']");
				} else {
					responseXML.append("'],");
				}
			}
			responseXML.append("]}}");				
		}else{
			responseXML.append(" {success:true,data:{list:[['','']]}}");
		}
		response.getWriter().write(responseXML.toString());
	}	
}
